package com.ysy.tz.mfg.controller;

import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.annotation.ShowLoading;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.event.BizEvent;
import com.ysy.tz.core.selfcomp.CommonDialog;
import com.ysy.tz.core.selfcomp.CusomterTableView;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.selfcomp.CustomerPagination;
import com.ysy.tz.core.utils.FormUtils;
import com.ysy.tz.core.utils.ModelUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.service.UserService;
import java.net.URL;
import java.util.HashMap;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;

public class UserController implements Initializable {
    @FXML
    private CusomterTableView userTableView;
    @FXML
    private TableColumn tb_funcPermissions;
    @FXML
    private CustomerPagination userTablePagination;
    @FXML
    private BorderPane queryForm;
    @Service
    UserService userService;
    ResourceBundle resources;
    static HashMap<String, String> funcMap = new HashMap();

    public UserController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        this.resources = resources;
        this.tb_funcPermissions.setCellFactory((col) -> {
            TableCell<UserEntity, String> cell = new TableCell<UserEntity, String>() {
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty && item != null) {
                        String[] funcPerms = item.split(",");
                        String value = "";
                        String[] var5 = funcPerms;
                        int var6 = funcPerms.length;

                        for(int var7 = 0; var7 < var6; ++var7) {
                            String funcPerm = var5[var7];
                            value = value + (String)UserController.funcMap.get(funcPerm) + ",";
                        }

                        this.setText(value.substring(0, value.length() - 1));
                    }

                }
            };
            return cell;
        });
    }

    @ShowLoading
    public void query() {
        UserEntity condition = null;

        try {
            condition = (UserEntity)FormUtils.collectParams(this.queryForm, UserEntity.class);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        condition.setPageNum(this.userTablePagination.getCurrentPageIndex() + 1);
        condition.setPageSize(this.userTablePagination.getPageSize());
        PageInfo<UserEntity> userList = this.userService.searchUserList(condition);
        ModelUtils.setTableViewValue(userList, this.userTableView, this.userTablePagination);
    }

    public void reset() {
        FormUtils.resetFrom(this.queryForm);
    }

    public void add() {
        try {
            CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_add"), "UserEdit", "User", new Object[0]).showAndWait();
            this.query();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public void changePwd() {
        UserEntity userEntity = (UserEntity)this.userTableView.getSelectionModel().getSelectedItem();
        if (userEntity == null) {
            CustomerAlert alert = new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0]);
            alert.showAndWait();
        } else {
            try {
                Dialog dialog = CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_changePwd"), "UserChangePwd", "User", new Object[]{userEntity});
                dialog.showAndWait();
                this.query();
            } catch (Exception var3) {
                var3.printStackTrace();
            }

        }
    }

    public void edit() {
        UserEntity userEntity = (UserEntity)this.userTableView.getSelectionModel().getSelectedItem();
        if (userEntity == null) {
            CustomerAlert alert = new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0]);
            alert.showAndWait();
        } else {
            try {
                Dialog dialog = CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_edit"), "UserEdit", "User", new Object[]{new BizEvent("user_Edit", userEntity)});
                dialog.setHeight(240.0);
                dialog.showAndWait();
                this.query();
            } catch (Exception var3) {
                var3.printStackTrace();
            }

        }
    }

    public void delete() {
        UserEntity userEntity = (UserEntity)this.userTableView.getSelectionModel().getSelectedItem();
        if (userEntity == null) {
            CustomerAlert alert = new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0]);
            alert.showAndWait();
        } else {
            UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            CustomerAlert confirmDialog;
            if (userEntity.getUserId().equals(currentUser.getUserId()) && userEntity.getEnterpriseId().equals(currentUser.getEnterpriseId())) {
                confirmDialog = new CustomerAlert(AlertType.WARNING, this.resources.getString("alert_userdel_warn"), new ButtonType[0]);
                confirmDialog.showAndWait();
            } else {
                confirmDialog = new CustomerAlert(AlertType.CONFIRMATION, ApplicationContext.commonBundle.getString("delete_confirm"), new ButtonType[0]);
                Optional<ButtonType> result = confirmDialog.showAndWait();
                if (result.get() == ButtonType.OK) {
                    this.userService.deleteUser(userEntity);
                    this.query();
                }

            }
        }
    }

    static {
        funcMap.put("DJ001", "用户管理");
        funcMap.put("DJ003", "羊胴体定级");
        funcMap.put("DJ004", "羊胴体查询");
        funcMap.put("DJ005", "系统参数");
        funcMap.put("DJ006", "羊胴体入库");
        funcMap.put("DJ007", "羊胴体出库");
    }
}
